package com.server.llh.llhserverwork.activity.base;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lelaohui.net.listener.NetDataListener;
import com.lelaohui.net.listener.ProgressListener;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.request.IRequest;
import com.lelaohui.net.utils.LLHCustomProgress;

/**
 * Created by ZTF on 2017-03-28.
 */

public abstract class BaseFragment extends Fragment implements ProgressListener {


    private INetRequestManager requestManager=null;
    protected abstract INetRequestManager getRequestManager();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(getLayoutView(), container, false);
        requestManager= getRequestManager();
        requestManager.setProgressListener(this);
        initView(view);
        doBases();
        return  view;
    }
    protected void requestData(@NonNull final IRequest request, @NonNull final NetDataListener dataCallback){
        requestManager.postRequest(request,dataCallback);
    }
    public abstract int getLayoutView();
    public abstract void initView(View view);
    public abstract void doBases();
    LLHCustomProgress myProgress=null;
    public void closeProgress() {
        try {
            if (myProgress == null || myProgress.isShowing()) {
                myProgress.dismiss();
            }
        }catch (Exception e){

        }
    }

    public void showProgress(Context context) {
        try {
        if (myProgress == null || !myProgress.isShowing()) {
            myProgress = LLHCustomProgress.show(context, "正在努力加载中.....", true,
                    null);
        }
        }catch (Exception e){

        }
    }
    /** Fragment当前状态是否可见 */
    protected boolean isVisible;
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if(getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }


    /**
     * 可见
     */
    protected void onVisible() {
        lazyLoad();
    }


    /**
     * 不可见
     */
    protected void onInvisible() {


    }


    /**
     * 延迟加载
     * 子类必须重写此方法
     */
    protected abstract void lazyLoad();
}
